package server;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyStore;
import com.sun.net.httpserver.*;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManagerFactory;

import java.io.*;

import javax.net.ssl.*;

public class HttpsServer {
    private int port;
    private String sslKeyPass;
    private String passWord;
    private com.sun.net.httpserver.HttpsServer httpsServer;


    HttpsServer(int port, String sslKeyPass, String passWord) {
        this.port = port;
        this.sslKeyPass = sslKeyPass;
        this.passWord = passWord;
        try {
            httpsServer = com.sun.net.httpserver.HttpsServer.create(new InetSocketAddress(port),0);
        }catch (Exception e){
            System.out.println(e);
        }
    }

    public HttpContext createContext(String context, HttpHandler httpHandler){
        return httpsServer.createContext(context, httpHandler);
    }

    public void start()  {
        try{
            // Create key manager
            char[] passphrase = passWord.toCharArray();
            KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(Files.newInputStream(Paths.get(sslKeyPass)), passphrase);

            // Create trust manager
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, passphrase);

            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(ks);

            // Initialize SSLContext
            SSLContext ssl = SSLContext.getInstance("TLS");
            ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

            httpsServer.setHttpsConfigurator (new HttpsConfigurator(ssl) {
                public void configure (HttpsParameters params) {
                    InetSocketAddress remote = params.getClientAddress();
                    SSLContext c = getSSLContext();
                    SSLParameters sslparams = c.getDefaultSSLParameters();
                    params.setSSLParameters(sslparams);
                }
            });
            httpsServer.setExecutor(null);
            httpsServer.start();
        }catch (Exception e){
            System.out.println(e);
        }
    }
    public void setExecutor(Executor var1){
        httpsServer.setExecutor(var1);
    }

}